package com.fowo.api.controller.shipment.wfs;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fowo.api.controller.purchase.vo.WarehouseBinParam;
import com.fowo.api.controller.purchase.vo.WarehouseBinStockVo;
import com.fowo.api.entity.WfsCargo;
import com.fowo.api.model.platformshipping.PlatformshippingSendoutgoodsAction;
import com.fowo.api.model.platformshipping.PlatformshippingVo;
import com.fowo.api.model.platformshipping.item.PlatformshippingItemItemVo;
import com.fowo.api.model.stock.out.StockOutVo;
import com.fowo.api.model.stock.out.product.StockOutProductItemVo;
import com.fowo.api.service.StockOutService;
import com.fowo.api.service.WfsCargoService;
import com.fowo.api.service.impl.PlatformshippingServiceImpl;
import com.fowo.api.service.impl.StockOutServiceImpl;
import com.fowo.api.service.impl.WarehouseBinDServiceImpl;
import com.fowo.api.service.impl.WfsCargoServiceImpl;
import com.fowo.api.user.model.JwtUserInfo;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class platformshippingSendoutgoodsAction implements PlatformshippingSendoutgoodsAction {
    @Resource
    private PlatformshippingServiceImpl platformshippingService;
    @Resource
    private WfsCargoServiceImpl wfsCargoService;
    @Resource
    private StockOutServiceImpl stockOutService;
    @Resource
    private WarehouseBinDServiceImpl warehouseBinDService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void run(List<Long> idList) throws Exception {
        for(int i=0;i<idList.size();i++){
            handle(idList.get(i));
        }
    }
    //发货
    private void handle(Long aLong) throws Exception {
        PlatformshippingVo voById = platformshippingService.getVoById(aLong);
        Assert.isTrue(voById.getShippingstatus().equals("1"),"违规操作");
        platformshippingService.lockStockOut(aLong);
        //更新货件shippingListCodes
        WfsCargo one = wfsCargoService.getOne(Wrappers.<WfsCargo>query().lambda().eq(WfsCargo::getShippingListCodes, voById.getShipmentCode()));
        if(one!=null){
            one.setShipmentStatus("是");
            one.setCargoSyncStatus("1");
            wfsCargoService.updateById(one);
        }
        PlatformshippingVo voById2 = platformshippingService.getVoById(aLong);
        //deliveryTime
        voById2.setDeliveryTime(new Date());
        //actualdeliverytime
        voById2.setActualdeliverytime(new Date());
        JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
        if (jwtUserInfo != null) {
            voById2.setSendgoodspeople(jwtUserInfo.getUserId());
        }
        //sendgoodspeople
        platformshippingService.updateById(voById2);
        //新增出库单
        addStockOut(voById);
    }

    private void addStockOut(PlatformshippingVo voById) throws Exception {
        //计算发货量的和
//        voById.getItems().
        StockOutVo model=new StockOutVo();
        model.setRemark(voById.getShipmentCode());
        model.setWarehouse(voById.getDeliverywarehouse());
        model.setType("CK006");
        model.setStatus("CS02");
        model.setCreateTime(voById.getCreateTime());
        model.setCreateUser(voById.getCreateUser());
        List<StockOutProductItemVo> stockOutProducts=new ArrayList<>();
        for (int i=0;i<voById.getItems().size();i++){
            PlatformshippingItemItemVo platformshippingItemItemVo = voById.getItems().get(i);
            StockOutProductItemVo stockOutProductItemVo=new StockOutProductItemVo();
            stockOutProductItemVo.setProductId(platformshippingItemItemVo.getProductName());
            stockOutProductItemVo.setSid(platformshippingItemItemVo.getShippingwarehousestore());//productGoodNum
            stockOutProductItemVo.setProductGoodNum(platformshippingItemItemVo.getShipmentsnum());
            stockOutProductItemVo.setStorageBinGoodId(platformshippingItemItemVo.getBinid());
            stockOutProducts.add(stockOutProductItemVo);
        }
        model.setStockOutProducts(stockOutProducts);
        stockOutService.create(model);
    }
}
